﻿Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
Imports CodeMatrix.Context
Imports System.Linq

Partial Class Apps_Shares_Uc_stp_auth_group_fv
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Try

                Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
                Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
                'Change mode by querystring
                If Not String.IsNullOrEmpty(Request.QueryString("ReadOnly")) Then
                    CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 37)
                ElseIf Not String.IsNullOrEmpty(Request.QueryString("Insert")) Then
                    CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 38)
                ElseIf Not String.IsNullOrEmpty(Request.QueryString("Edit")) Or String.IsNullOrEmpty(Request.QueryString("Update")) Then
                    CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 39)
                End If

                If Not String.IsNullOrEmpty(Request.QueryString("Edit")) Then
                    EditView()
                End If
            Catch ex As Exception
                GenericHelper.ShowAlert(Me, ex.Message)
            End Try
        End If
    End Sub

    Private Sub EditView()

        ' Modified by Boonyarit Yingvoragan
        ' Modified date 24/10/2010
        Try
            If Not String.IsNullOrEmpty(Request.QueryString("readonly")) Then
                'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "asdf", " $('.td_data_end').hide(); $('input').attr('disabled','disabled'); ", True)
                Me.LoadAuthenGroup(Request.QueryString("Edit"), 0)
            Else
                Me.LoadAuthenGroup(Request.QueryString("Edit"), 0, 2)
            End If

            ltrGroup.Text = CodeMatrix.Business.GenericData.GetGroupUserLabel(Me, Request.QueryString("Edit"))

            t1.Style("display") = "none"
            t2.Style("display") = "none"
            t3.Style("display") = "none"

            ShowGroup.Visible = True
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click

        ' Commented by Boonyarit Yingvoragan
        ' Commented date 24/10/2010
        ' Not used
        'Dim scrpt = String.Format(" try{{loadAuthDepartmentObject({0}, 0, 'pnl');}} catch(x){{alert(x)}} ", ddlSystem.SelectedValue, _
        '                          lbDepartment.SelectedValue)
        'ScriptManager.RegisterStartupScript( _
        '    Me, Me.GetType(), Guid.NewGuid.ToString(), scrpt, True _
        ')

        ' Modified by Boonyarit Yingvoragan
        ' Modified date 24/10/2010
        Try
            Me.LoadAuthenGroup(ddlGroupUser.SelectedValue, 0, 1)
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Public WebServiceURL As String
    Public rowTotal As Int32
    Public colTotal As Int32

    Protected Sub LoadAuthenGroup(ByVal GroupCode As Int32, ByVal MainSystemCode As Int32, Optional ByVal Mode As Int16 = 0)

        ' Mode
        ' 0 - ReadOnly
        ' 1 - Insert
        ' 2 - Update
        Try
            If Mode = 1 Then WebServiceURL = "../WebService.asmx/CheckAndSaveAuthenGroupObject"
            If Mode = 2 Then WebServiceURL = "../WebService.asmx/SaveAuthenGroupObject"

            Dim tblAuthenMenuSystem = CodeMatrix.Utilities.CodeHelper.GetDataTable("exec USP_GetAuthenGroup @staff_code, @group_code, @main_system_code", _
                                                                   New SqlParameter("@staff_code", HttpContext.Current.Session("staff_code")), _
                                                                   New SqlParameter("@group_code", GroupCode), _
                                                                   New SqlParameter("@main_system_code", MainSystemCode))

            Dim tblMasterType = CodeMatrix.Utilities.CodeHelper.GetDataTable("exec USP_List_master_type @type_status", New SqlParameter("@type_status", 1))
            Dim tblMenuSystem = CodeMatrix.Utilities.CodeHelper.GetDataTable("exec USP_GetMenuSystem @main_system_code", New SqlParameter("@main_system_code", MainSystemCode))

            rowTotal = tblMenuSystem.Rows.Count
            colTotal = tblMasterType.Rows.Count

            If (tblAuthenMenuSystem.Rows.Count = 0) Then Return

            If (tblMasterType.Rows.Count > 0) Then

                ' Start Generate table
                ltrMenuSystemTable.Text = "<table id='idMenuSystemTable' cellspacing=""2"" cellpadding=""2"" bordercolor=""#999999"" border=""0"" align=""center"" width=""100%"">"

                Dim colEmpty As String = "<td bgcolor=""#D8D8D8"" class=""td_data"">&nbsp;</td>"
                Dim rowEmpty As String = ""

                ' Generate Header
                ltrMenuSystemTable.Text &= "<tbody><tr bgcolor=""#ff9999"" align=""center""  class=""component_caption""><td  class=""th_data"" colspan=""2"">ระบบ/เมนูระบบ</td>"
                Dim i As Int32
                Dim j As Int32
                Dim k As Int32 = 0
                For i = 0 To tblMasterType.Rows.Count - 1 Step 1
                    If Mode = 0 Then
                        ltrMenuSystemTable.Text &= String.Format("<td class=""th_data"">{0}</td>", tblMasterType.Rows(i)("type_name").ToString())
                    Else
                        ltrMenuSystemTable.Text &= String.Format("<td class=""th_data""><input type=""checkbox"" id='col" & i & "' onclick=""checkAllCol(" & i & ", " & rowTotal & ");isCheckAll(" & rowTotal & ", " & colTotal & ");"" >{0}</td>", tblMasterType.Rows(i)("type_name").ToString())
                    End If
                    rowEmpty &= colEmpty
                Next
                ltrMenuSystemTable.Text &= "</tr>"

                If (tblAuthenMenuSystem.Rows.Count > 0) Then
                    Dim main_system_code As Int32 = -1
                    For i = 0 To tblMenuSystem.Rows.Count - 1 Step 1
                        Dim rowMenu As DataRow = tblMenuSystem.Rows(i)
                        Dim rowAuthenMenu As DataRow() = tblAuthenMenuSystem.Select("menu_system_code = " & rowMenu("menu_system_code"))
                        If rowAuthenMenu.Count() > 0 Then
                            ' Main System
                            If main_system_code <> rowMenu("main_system_code") Then
                                ltrMenuSystemTable.Text &= String.Format("<tr><td bgcolor=""#D8D8D8"" colspan=""2"" class=""td_data"">{0}</td>{1}</tr>", rowMenu("main_system_name").ToString(), rowEmpty)
                            End If
                            If Mode = 0 Then
                                ltrMenuSystemTable.Text &= String.Format("<tr><td colspan=""2"" class=""td_data"">{0}</td>", rowAuthenMenu(0)("menu_system_name").ToString())
                            Else
                                ltrMenuSystemTable.Text &= String.Format("<tr><td colspan=""2"" class=""td_data""><input type=""checkbox"" id='row" & i & "' onclick=""checkAllRow(" & i & ", " & colTotal & ");isCheckAll(" & rowTotal & ", " & colTotal & ");"" >{0}</td>", rowAuthenMenu(0)("menu_system_name").ToString())
                            End If
                            For k = 0 To tblMasterType.Rows.Count - 1 Step 1
                                Dim rowMasterType As DataRow = tblMasterType.Rows(k)
                                Dim rowMenuType As DataRow() = tblAuthenMenuSystem.Select("menu_system_code = " & rowMenu("menu_system_code") & " and type_code = " & rowMasterType("type_code"))
                                If rowMenuType.Count() > 0 Then
                                    Dim checked As String = IIf(rowMenuType(0)("has_authen"), "checked=""checked""", "")
                                    ' Read only mode
                                    If Mode = 0 Then ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'><input class=""cb_data"" {0} disabled=""disabled"" type=""checkbox"" />&nbsp;</td>", checked)
                                    ' Insert mode
                                    If Mode = 1 Then ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'><input class=""cb_data"" type=""checkbox"" authenDepCode=""{0}"" authenGroupCode=""0"" id='" & i & "-" & k & "' onclick='isCheckAll(" & rowTotal & ", " & colTotal & ");' />&nbsp;</td>", rowMenuType(0)("auth_department_code"))
                                    ' Edit mode
                                    If Mode = 2 Then ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'><input class=""cb_data"" {0} type=""checkbox"" authenDepCode=""{1}"" authenGroupCode=""{2}"" id='" & i & "-" & k & "' onclick='isCheckAll(" & rowTotal & ", " & colTotal & ");' />&nbsp;</td>", checked, rowMenuType(0)("auth_department_code"), rowMenuType(0)("auth_group_code"))
                                Else
                                    ltrMenuSystemTable.Text &= String.Format("<td align='center' class='td_data' bordercolor='#CCCCCC'>&nbsp;</td>")
                                End If
                            Next
                            ltrMenuSystemTable.Text &= "</tr>"
                            main_system_code = rowMenu("main_system_code")
                        End If
                    Next

                    If Mode = 0 Then
                        ltrMenuSystemTable.Text &= String.Format("<tr><td align=""center"" bgcolor=""#ffffff"" class=""td_data_end"" colspan=""{0}""><center><input type=""button"" onclick=""window.history.back();"" value=""ย้อนกลับ"" /></center></td></tr>", tblMasterType.Rows.Count + 2)
                    ElseIf Mode = 1 Then
                        ltrMenuSystemTable.Text &= String.Format("<tr><td align=""center"" bgcolor=""#ffffff"" class=""td_data_end"" colspan=""{0}""><center><input type=""button"" onclick=""saveAuthen()"" value=""บันทึกข้อมูล"" /></center></td></tr>", tblMasterType.Rows.Count + 2)
                    Else
                        ltrMenuSystemTable.Text &= String.Format("<tr><td align=""center"" bgcolor=""#ffffff"" class=""td_data_end"" colspan=""{0}""><center><input type=""button"" onclick=""saveAuthen()"" value=""บันทึกข้อมูล"" /><input type=""reset"" id=""btnCancel"" title=""ยกเลิก"" value=""ยกเลิก"" onclick=""window.location='stp_auth_group_list.aspx?Edit=true';"" /></center></td></tr>", tblMasterType.Rows.Count + 2)
                    End If
                End If

                ltrMenuSystemTable.Text &= "</tbody></table>"

            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        ddlGroupUser.SelectedIndex = -1
        ltrMenuSystemTable.Text = ""
    End Sub
End Class
